//
//  DataBaseHandler.m
//  Foodie_Talk_ProjectA
//
//  Created by Ming on 15-6-10.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "DataBaseHandler.h"
#import "MakeDetailObject.h"
@implementation DataBaseHandler

+ (DataBaseHandler *)shareInstance
{
    static DataBaseHandler *dbHandler = nil;
    if (nil == dbHandler) {
        dbHandler = [[DataBaseHandler alloc] init];
        [dbHandler openBD];
        [dbHandler createFavoritesTable];
    }
    return dbHandler;
}

- (BOOL)openBD
{
    NSString *docpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *path = [docpath stringByAppendingPathComponent:@"favorites.db"];
    NSLog(@"000000000000-------%@", path);
    int result = sqlite3_open([path UTF8String], &dbPoint);
    return [self judgeResult:result withTitle:@"打开数据库"];
}


- (BOOL)closeBD
{
    int result = sqlite3_close(dbPoint);
    return [self judgeResult:result withTitle:@"关闭数据库"];
}


- (BOOL)createFavoritesTable
{
    NSString *sql = [NSString stringWithFormat:@"create table favorites (codeId text primary key, healthStr text, remark text, num text, info text, img text, name text, allClick text, content text, favorites text)"];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result withTitle:@"创建数据表"];
}


- (BOOL)insertFavorites:(MakeDetailObject *)make
{
    NSString *sql = [NSString stringWithFormat:@"insert into favorites values ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", make.codeId, make.healthStr, make.remark, make.num, make.info, make.img, make.name, make.allClick, make.content, make.favorites];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result withTitle:@"插入数据"];
}



- (BOOL)deleteFavoritesWithCodeId:(NSString *)codeId
{
    NSString *sql = [NSString stringWithFormat:@"delete from favorites where codeId = '%@'", codeId];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    NSLog(@"此时数据库表中的个数有=======%ld", [self selectAll].count);
    return [self judgeResult:result withTitle:@"删除数据"];
}



- (NSMutableArray *)selectAll
{
    sqlite3_stmt *stmt = nil;
    NSString *sql = [NSString stringWithFormat:@"select * from favorites"];
    int result = sqlite3_prepare_v2(dbPoint, [sql UTF8String], -1, &stmt, NULL);
    NSMutableArray *tempArray = [NSMutableArray array];
    if (SQLITE_OK == result) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            MakeDetailObject *makeClass = [[MakeDetailObject alloc] init];
           
            
            makeClass.codeId = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            
            
             makeClass.healthStr = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
            
             makeClass.remark = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
            
             makeClass.num = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
            
             makeClass.info = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];
            
             makeClass.img = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 5)];
            
             makeClass.name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 6)];
            
             makeClass.allClick = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 7)];
            
             makeClass.content = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 8)];
            
             makeClass.favorites = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 9)];
            
            [tempArray addObject:makeClass];
            [makeClass release];
        }
    }
    sqlite3_finalize(stmt);
    return tempArray;
}

- (BOOL)judgeResult:(int)result withTitle:(NSString *)title
{
    if (SQLITE_OK == result) {
        NSLog(@"%@ 成功", title);
        return YES;
    }else {
        NSLog(@"%@ 失败， 原因：%d", title, result);
        return NO;
    }
}

@end
